//
#include "tsp.h"
#include <iostream>

MTD::MTD(const std::string& pull_address, const std::string& push_address)
    : pull_address(pull_address), push_address(push_address) {
    pull_socket = zmq::socket_t(context, zmq::socket_type::pull);
    pull_socket.connect(pull_address);
    
    push_socket = zmq::socket_t(context, zmq::socket_type::push);
    push_socket.bind(push_address);
}

MTD::~MTD() {
    pull_socket.close();
    push_socket.close();
}

void MTD::startup() {
    while (true) {
        zmq::message_t message;
        try {
            pull_socket.recv(message, zmq::recv_flags::none);
        } catch (const zmq::error_t& e) {
            std::cerr << "ZMQ error: " << e.what() << std::endl;
            break;
        }
        
        uint8_t* input = static_cast<uint8_t*>(message.data());
        std::cout << "动目标检测：" << input[0] << ", " << input[1] << "; ???????????" << std::endl;
        uint8_t output[1024];
        output[0] = 88;
        output[1] = 89;
        zmq::message_t response(sizeof(uint8_t) * 1024);
        memcpy(response.data(), output, sizeof(uint8_t) * 1024);
        
        try {
            push_socket.send(response, zmq::send_flags::none);
        } catch (const zmq::error_t& e) {
            std::cerr << "ZMQ error: " << e.what() << std::endl;
            break;
        }
    }
}

void MTD::performMTD(float* input, float* output, size_t size) {
    // 简化的MTD算法示例
    for (size_t i = 0; i < size; ++i) {
        output[i] = input[i] > 0.5f ? input[i] : 0; // 简化的门限检测
    }
}